<template>
  <el-dialog title="选择单词" :visible.sync="localOpen" width="800px" :close-on-click-modal="true" @close="cancel"
    append-to-body>
  <div>
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="单词" prop="word">
        <el-input v-model="queryParams.word" placeholder="请输入单词" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>



    <el-table v-loading="loading" ref="selectWordTable" :data="wordsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="id" />
      <el-table-column label="单词" align="center" prop="word" />
      <el-table-column label="图片数量" align="center" prop="imageCount" />
      <el-table-column label="音频数量" align="center" prop="audioCount" />
      <el-table-column label="字母" align="center" prop="letter" />
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />
  </div>
  <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="selectData">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>

  </el-dialog>
</template>

<script>
  import {
    mapState
  } from 'vuex';
  import store from "@/store";

  import {
    listWords,
  } from "@/api/bus_web/thesaurus/words";
  export default {
    name: "SelectWords",
    dicts: ['common_state'],
    props: {
      open: Boolean
    },
    data() {
      return {
        // 遮罩层
        loading: true,
        localOpen: false,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 单词管理表格数据
        wordsList: [],
        // 弹出层标题
        title: "",
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          word: null,
          wordType:1,
        },
      };
    },

    computed: {
      ...mapState({
        word: state => state.selectWord.word,
        wordSelectModel: state => state.selectWord.wordSelectModel,
      })
    },
    watch: {
      open(newValue, oldValue) {
        this.localOpen = newValue; // 同步父组件的 open 值到本地状态
      },
      localOpen(newValue) {
        if (newValue) {
          this.getList() // 当本地状态变为 false 时，通知父组件
        } else {
          this.cancel()
        }
      }
    },
    methods: {
      //取消
      cancel(){
        this.queryParams= {
          pageNum: 1,
          pageSize: 10,
          word: null,
          wordType:1,
        }
        this.$emit('close-select-window');
      },
      /** 查询单词管理列表 */
      getList() {
        this.loading = true;
        listWords(this.queryParams).then(response => {
          this.wordsList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {

        if(selection.length>1 && this.wordSelectModel==2){
          selection=[]
          this.$refs.selectWordTable.clearSelection()
          this.$store.dispatch('selectWord/cleanWordData')
          this.$modal.msgError("最多只能选择1条数据")
          return
        }
        selection.map(item => {
          this.$store.dispatch('selectWord/setWord', item)
        })
      },
      //确定选择数据
      selectData() {
        if (this.word.length < 1) {
          this.$modal.msgError("请至少选中1条数据")
          return
        }
        this.$emit('selected-data')
      }
    }
  };
</script>
